package org.jeecg.modules.qy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.utils.UuidUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.NumberUtils;
import org.jeecg.common.util.oss.OssBootUtil;
import org.jeecg.modules.qy.entity.QyAccount;
import org.jeecg.modules.qy.entity.QyCommodity;
import org.jeecg.modules.qy.entity.QySourceCodeCreate;
import org.jeecg.modules.qy.entity.QySourceCodeCreateList;
import org.jeecg.modules.qy.entity.vo.CodeCreateVo;
import org.jeecg.modules.qy.mapper.QySourceCodeCreateMapper;
import org.jeecg.modules.qy.service.IQyAccountService;
import org.jeecg.modules.qy.service.IQyCommodityService;
import org.jeecg.modules.qy.service.IQySourceCodeCreateListService;
import org.jeecg.modules.qy.service.IQySourceCodeCreateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 溯源码列表
 * @Author: jeecg-boot
 * @Date:   2021-03-15
 * @Version: V1.0
 */
@Service
public class QySourceCodeCreateServiceImpl extends ServiceImpl<QySourceCodeCreateMapper, QySourceCodeCreate> implements IQySourceCodeCreateService {



    @Autowired
    private IQyCommodityService commodityService;


    @Autowired
    private IQySourceCodeCreateListService sourceCodeCreateListService;

    @Autowired
    private IQyAccountService accountService;


    @Override
    public IPage<CodeCreateVo> findCreateCodeList(Page<CodeCreateVo> page, QueryWrapper<CodeCreateVo> eWrapper){
        return baseMapper.selectCreateCodeList(page,eWrapper);
    }

    @Override
    public Result<String> saveSourceCode(QySourceCodeCreate sourceCode){
        QyAccount account = accountService.getByCompanyId(sourceCode.getCompanyId());
        if(account.getMaxCodeNum()<sourceCode.getCodeNum()){
            return Result.ERROR("单次最多可生成"+account.getMaxCodeNum()+"个码");
        }
        //先查询出对应的商品
        QyCommodity commodity = commodityService.getById(sourceCode.getCommodityId());
        if(commodity.getCommodityType()==1){
            //一物一码
            if(sourceCode.getCodeNum()>commodity.getOneCodeNum()){
                return Result.ERROR("一物一码生成数量不足");
            }else{
                commodity.setOneCodeNum(commodity.getOneCodeNum()-sourceCode.getCodeNum());
            }
        }else{
            if(sourceCode.getCodeNum()>commodity.getMuchCodeNum()){
                return Result.ERROR("N物一码生成数量不足");
            }else{
                commodity.setMuchCodeNum(commodity.getMuchCodeNum()-sourceCode.getCodeNum());
            }
        }

        //生成12位批次号
        String batchNo = NumberUtils.createBatchNo();
        sourceCode.setBatchNo(batchNo);
        sourceCode.setSectionNo(sourceCode.getBatchNo()+NumberUtils.autoGenericCode("0",6)+"-"+sourceCode.getBatchNo()+NumberUtils.autoGenericCode(sourceCode.getCodeNum()-1+"",6));
        sourceCode.setId(UuidUtils.getUUID());
        if(commodity.getCommodityType()==1){
            sourceCode.setStatus(0);
            save(sourceCode);
            //异步调用保存码的方法
            sourceCodeCreateListService.saveAsyncCodeList(sourceCode,commodity);
        }else{
            sourceCode.setStatus(1);
            save(sourceCode);
            sourceCodeCreateListService.saveCodeList(sourceCode,commodity);
        }

        return Result.OK();
    }




}
